package com.bumptech.glide.load.engine;

import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import o.rz;
import o.sa;

public final class GlideException extends Exception {
    private static final StackTraceElement[] ˊ = new StackTraceElement[0];
    private String ʻ;
    private rz ˋ;
    private Class<?> ˎ;
    private sa ˏ;
    private final List<Throwable> ॱ;
    @Nullable
    private Exception ᐝ;

    static final class d implements Appendable {
        private boolean ˋ = true;
        private final Appendable ˏ;

        d(Appendable appendable) {
            this.ˏ = appendable;
        }

        public Appendable append(char c) throws IOException {
            boolean z = false;
            if (this.ˋ) {
                this.ˋ = false;
                this.ˏ.append("  ");
            }
            if (c == '\n') {
                z = true;
            }
            this.ˋ = z;
            this.ˏ.append(c);
            return this;
        }

        public Appendable append(@Nullable CharSequence charSequence) throws IOException {
            CharSequence ˏ = ˏ(charSequence);
            return append(ˏ, 0, ˏ.length());
        }

        public Appendable append(@Nullable CharSequence charSequence, int i, int i2) throws IOException {
            boolean z = false;
            CharSequence ˏ = ˏ(charSequence);
            if (this.ˋ) {
                this.ˋ = false;
                this.ˏ.append("  ");
            }
            if (ˏ.length() > 0 && ˏ.charAt(i2 - 1) == '\n') {
                z = true;
            }
            this.ˋ = z;
            this.ˏ.append(ˏ, i, i2);
            return this;
        }

        @NonNull
        private CharSequence ˏ(@Nullable CharSequence charSequence) {
            if (charSequence == null) {
                return "";
            }
            return charSequence;
        }
    }

    public GlideException(String str) {
        this(str, Collections.emptyList());
    }

    public GlideException(String str, Throwable th) {
        this(str, Collections.singletonList(th));
    }

    public GlideException(String str, List<Throwable> list) {
        this.ʻ = str;
        setStackTrace(ˊ);
        this.ॱ = list;
    }

    public void ˏ(sa saVar, rz rzVar) {
        ˊ(saVar, rzVar, null);
    }

    public void ˊ(sa saVar, rz rzVar, Class<?> cls) {
        this.ˏ = saVar;
        this.ˋ = rzVar;
        this.ˎ = cls;
    }

    public void ˎ(@Nullable Exception exception) {
        this.ᐝ = exception;
    }

    public Throwable fillInStackTrace() {
        return this;
    }

    public List<Throwable> ॱ() {
        return this.ॱ;
    }

    public List<Throwable> ˊ() {
        List arrayList = new ArrayList();
        ॱ((Throwable) this, arrayList);
        return arrayList;
    }

    public void ˎ(String str) {
        List ˊ = ˊ();
        int size = ˊ.size();
        for (int i = 0; i < size; i++) {
            Log.i(str, "Root cause (" + (i + 1) + " of " + size + ")", (Throwable) ˊ.get(i));
        }
    }

    private void ॱ(Throwable th, List<Throwable> list) {
        if (th instanceof GlideException) {
            for (Throwable ॱ : ((GlideException) th).ॱ()) {
                ॱ(ॱ, (List) list);
            }
            return;
        }
        list.add(th);
    }

    public void printStackTrace() {
        printStackTrace(System.err);
    }

    public void printStackTrace(PrintStream printStream) {
        ˊ(printStream);
    }

    public void printStackTrace(PrintWriter printWriter) {
        ˊ(printWriter);
    }

    private void ˊ(Appendable appendable) {
        ॱ((Throwable) this, appendable);
        ˊ(ॱ(), new d(appendable));
    }

    public String getMessage() {
        StringBuilder append = new StringBuilder(71).append(this.ʻ).append(this.ˎ != null ? ", " + this.ˎ : "").append(this.ˋ != null ? ", " + this.ˋ : "").append(this.ˏ != null ? ", " + this.ˏ : "");
        List<Throwable> ˊ = ˊ();
        if (ˊ.isEmpty()) {
            return append.toString();
        }
        if (ˊ.size() == 1) {
            append.append("\nThere was 1 cause:");
        } else {
            append.append("\nThere were ").append(ˊ.size()).append(" causes:");
        }
        for (Throwable th : ˊ) {
            append.append('\n').append(th.getClass().getName()).append('(').append(th.getMessage()).append(')');
        }
        append.append("\n call GlideException#logRootCauses(String) for more detail");
        return append.toString();
    }

    private static void ॱ(Throwable th, Appendable appendable) {
        try {
            appendable.append(th.getClass().toString()).append(": ").append(th.getMessage()).append('\n');
        } catch (IOException e) {
            throw new RuntimeException(th);
        }
    }

    private static void ˊ(List<Throwable> list, Appendable appendable) {
        try {
            ˏ((List) list, appendable);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    private static void ˏ(List<Throwable> list, Appendable appendable) throws IOException {
        int size = list.size();
        for (int i = 0; i < size; i++) {
            appendable.append("Cause (").append(String.valueOf(i + 1)).append(" of ").append(String.valueOf(size)).append("): ");
            Throwable th = (Throwable) list.get(i);
            if (th instanceof GlideException) {
                ((GlideException) th).ˊ(appendable);
            } else {
                ॱ(th, appendable);
            }
        }
    }
}
